package com.rao.gmall.web.all.config;

import com.rao.gmall.common.cache.GmallCache;
import com.rao.gmall.common.cache.GmallCacheAspect;
import com.rao.gmall.common.config.ItemRedissonConfig;
import com.rao.gmall.common.config.MybatisPlusConfig;
import com.rao.gmall.common.handler.GlobalExceptionHandler;
import com.rao.gmall.feign.AppFeignConfig;
import lombok.Data;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author nzmxfan
 * @create 2021-08-13-0:52
 */
@EnableAspectJAutoProxy
@AutoConfigureAfter(RedisAutoConfiguration.class)
@Configuration
@Import({GmallCacheAspect.class, AppFeignConfig.class, GlobalExceptionHandler.class, MybatisPlusConfig.class})
public class ServiceWebConfig {

    //  第二步： 加载 布隆过滤器对象初始化的类，自动注入，供 SkuInfoServiceImpl 添加 sku 时让布隆过滤器添加占位;
// 定义属于自己得业务线程池（不要抽取，业务可能不同需求不同）



        // 将自定义类作为参数传入自定义的业务线程池，且将线程池命名为 corePool
        @Bean("wellPool")
        public ThreadPoolExecutor executor(ThreadConfigProperties properties) {
            return new ThreadPoolExecutor(properties.core,
                    properties.max,
                    properties.keepalive,
                    TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<>(properties.queueLength),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.AbortPolicy());
        }

        // 读取配置文件的属性并创建 set、get 方法
        @Data
        @Component
        @ConfigurationProperties(prefix = "well-service.thread")
        class ThreadConfigProperties {
            private int core;
            private int max;
            private Long keepalive;
            private int queueLength;
        }
}
